package com.lc.kafka.simple;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lchown
 * @since 2024/4/4
 */
public class Consumer {
    public static void main(String[] args) {
        //配置
        Map<String,Object> config = new HashMap<>();
        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
        config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        config.put(ConsumerConfig.GROUP_ID_CONFIG,"itguigu");

        //消费者
        KafkaConsumer<String,String> consumer = new KafkaConsumer<>(config);
        //订阅
        consumer.subscribe(Collections.singletonList("test"));

        //接受消息
        while (true){
            //一秒拉取一次
            ConsumerRecords<String, String> poll = consumer.poll(1000);
            for (ConsumerRecord<String,String> consumerRecord :poll){
                System.out.println(consumerRecord);
            }
        }


    }
}
